package com.example.mydemo.service;

import com.example.mydemo.dao.UserMapper;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    UserMapper userMapper;

//    @GlobalTransactional
    public int update(Integer count) {

//        修改
        String s = updateClass(count);
        System.out.println("调用远程服务返回结果： " + s);

//        修改
        int update = userMapper.update(count);
        System.out.println("本地修改数据： " + update);

        return 1;
    }


    public String updateClass(Integer count){
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type", "application/json");
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("count", count);
        HttpEntity httpEntity = new HttpEntity(map, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:8080/updateClass", httpEntity, String.class);
        return stringResponseEntity.getBody();
    }

}
